﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogLib;
using System.IO;

namespace PhotoNamingService
{
    public class YWBHCreatorDefault : IYWBHCreator
    {
        public string InterfaceName
        {
            get
            {
                return this.GetType().Name;
            }
        }

        private string _error = "";
        public string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        public YWBHCreatorDefault()
        {
           
        }

        protected string HandleIndex(int index)
        {
            if (index >= 0 && index <= 999)
            {
                return index.ToString().PadLeft(3, '0');
            }
            else
            {
                throw new Exception("当日序列号已超出指定范围(0~999)");
            }
        }

        public string GetYWBH()
        {
            // KB + 4位设备编号 + yyMMdd + 3位流水号      
            string serialNumber = "";
            int index = IndexConfig.GetYWBHIndex();
            Log.WriteAsync("当前序列号:" + index.ToString());
            string date = DateTime.Now.ToString("yyyyMMdd");
            date = date.Substring(2, 6);
            string strIndex = HandleIndex(index);
            serialNumber = string.Format("KB{0}{1}{2}",
                ProductConfig.Instance.DeviceID, 
                date,
                strIndex);
            Log.WriteAsync("产生序列号:" + serialNumber);
            return serialNumber;
        }
    }
}
